package com.wz.jiangsu.main.opengimini;

import okhttp3.OkHttpClient;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;

import javax.net.ssl.*;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

public class OpenGeminiWriteDemoWithAuth {

    public static void main(String[] args) {

        // OpenGemini/InfluxDB 的 URL 和认证信息
        final String serverURL = "https://10.25.10.239:8000";  // 修改为实际的 OpenGemini 地址和端口
        final String username = "root";
        final String password = "root";

        // 创建一个 OkHttpClient，并禁用 SSL 证书验证
//        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
//        disableSSLCertificateValidation(httpClientBuilder);  // 调用禁用 SSL 证书验证的方法aa

        // 连接到 OpenGemini
//        InfluxDB influxDB = InfluxDBFactory.connect(serverURL, username, password, httpClientBuilder);
//        InfluxDB influxDB = InfluxDBFactory.connect(serverURL, username, password);


        InfluxDB influxDB = InfluxDBFactory.connect(serverURL, username, password);

        influxDB.setDatabase("test");

        // 测量写入数据点的时间
        long startTime = System.nanoTime(); // 开始时间

        // 构建并写入一个数据点
        influxDB.write(Point.measurement("info_wz_table102")  // 测量名称
                .tag("location", "us-midwest")  // 添加标签
                .addField("temperature", 82)    // 添加字段
                .time(1465839830100400205L, TimeUnit.NANOSECONDS)  // 设置时间戳（根据需要设置时间单位）
                .build());

        long endTime = System.nanoTime(); // 结束时间
        long duration = endTime - startTime; // 计算写入操作所用时间

        System.out.println("数据写入耗时: " + duration + " 纳秒");

        // 关闭连接
        influxDB.close();
    }

    /**
     * 禁用 SSL 证书验证的方法。
     *
     * @param httpClientBuilder OkHttpClient 的构建器
     */
    private static void disableSSLCertificateValidation(OkHttpClient.Builder httpClientBuilder) {
        try {
            // 创建一个信任所有证书的 TrustManager
            final TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        public X509Certificate[] getAcceptedIssuers() {
                            return new X509Certificate[]{};
                        }

                        public void checkClientTrusted(X509Certificate[] chain, String authType) {}

                        public void checkServerTrusted(X509Certificate[] chain, String authType) {}
                    }
            };

            // 设置 SSL Context
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

            // 创建一个不进行验证的 HostnameVerifier
            final HostnameVerifier allHostsValid = (hostname, session) -> true;

            // 配置 OkHttpClient 以禁用 SSL 证书验证
            httpClientBuilder.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0])
                    .hostnameVerifier(allHostsValid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
